<?php
/**
 * jishupu-cms
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2022/3/16
 */

namespace App\Services;


use App\Models\Info;
use App\Models\InfoCategory;

/**
 * 站点meta，以及使用率较高的信息模块的页面meta配置
 *
 * Class MetaService
 * @package App\Services
 */
class MetaService
{

    /**
     * 站点seo
     * @return array
     */
    static function siteMeta()
    {
        $getSiteConfig = ConfigService::getSiteConfig();
        $name = trim($getSiteConfig['name']);
        $slogan = trim($getSiteConfig['slogan']);
        if ($name && $slogan) {
            $name .= "-" . $slogan;
        }
        $keywords = $getSiteConfig['keywords'];
        $description = $getSiteConfig['description'];
        return ['title' => $name, 'keywords' => $keywords, 'description' => $description];
    }

    /**
     * 栏目seo
     * @param InfoCategory $cate
     * @return array
     */
    static function cateMeta(InfoCategory $cate)
    {
        $siteMeta = self::siteMeta();
        $or = trim($cate->keywords) || trim($cate->description);
        $keywords = $or ? $cate->keywords : $siteMeta['keywords'];
        $description = $or ? $cate->description : $siteMeta['description'];
        $name = $cate->type == 3 && $cate->infoPage ? $cate->infoPage->title : $cate->name;
        return ['title' => $name . "-" . $siteMeta['title'], 'keywords' => $keywords, 'description' => $description];
    }

    /**
     * 信息模块seo
     * @param Info $info
     * @return array
     */
    static function infoMeta(Info $info)
    {
        $siteMeta = self::siteMeta();
        $or = trim($info->keywords) || trim($info->description);
        $keywords = $or ? $info->keywords : $siteMeta['keywords'];
        $description = $or ? $info->description : $siteMeta['description'];
        $cateMeta = self::cateMeta($info->infoCategory);
        return ['title' => $info->title . "-" . $cateMeta['title'], 'keywords' => $keywords, 'description' => $description];
    }
}
